<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="6">
            <a-form-item label="客户代码">
              <a-input placeholder="请输入客户代码" v-model="queryParam.serial"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="6">
            <a-form-item label="客户名称">
              <a-input placeholder="请输入客户名称" dict="hf_custom,name,name" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
      <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('客户信息')">导出</a-button>
      <a-upload
        name="file"
        :showUploadList="false"
        :multiple="false"
        :headers="tokenHeader"
        :action="importExcelUrl"
        @change="handleImportExcel"
      >
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <a-button
        style="background: #FF8F00;border: #FF8F00;"
        icon="import"
        type="primary"
        @click="uploadModalJD.visible = true"
        >金蝶导入
      </a-button>
      <a-modal
        :visible="uploadModalJD.visible"
        @cancel="
          () => {
            uploadModalJD.visible = false
          }
        "
      >
        <a-spin :spinning="uploadModalJD.loading">
          <a-row :gutter="[16, 16]">
            <a-col :span="24">
              <a-upload :multiple="false" :showUploadList="false" name="file" :customRequest="customUpload">
                <a-button icon="import" type="primary">选择金蝶导出的文件</a-button>
              </a-upload>
            </a-col>
          </a-row>
        </a-spin>
      </a-modal>

      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete" />
            删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a @click="handleDelete(record.id)">删除</a>
        </span>
      </a-table>
    </div>
    <hf-custom-modal ref="modalForm" @ok="modalFormOk"></hf-custom-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import HfCustomModal from './modules/HfCustomModal'
import { postAction } from '@api/manage'

export default {
  name: 'HfCustomList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    HfCustomModal
  },
  data() {
    return {
      description: '客户信息管理页面',
      uploadModalJD: {
        visible: false,
        loading: false
      },
      isorter: {
        column: 'serial',
        order: 'asc'
      },
      // 表头
      columns: [
        {
          title: '代码',
          align: 'center',
          dataIndex: 'serial'
        },
        {
          title: '客户名称',
          align: 'center',
          dataIndex: 'name'
        },
        {
          title: '地址',
          align: 'center',
          dataIndex: 'address'
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/sale/hfCustom/list',
        delete: '/sale/hfCustom/delete',
        deleteBatch: '/sale/hfCustom/deleteBatch',
        exportXlsUrl: '/sale/hfCustom/exportXls',
        importExcelUrl: 'sale/hfCustom/importExcel',
        importExcelUrlByJD: 'sale/hfCustom/importExcelByJD'
      },
      dictOptions: {},
      superFieldList: []
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
    importExcelUrlByJD: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrlByJD}`
    }
  },
  methods: {
    customUpload(options) {
      this.uploadModalJD.loading = true
      const { file, onProgress, onSuccess, onError } = options
      const formData = new FormData()
      formData.append('file', file)
      postAction(this.importExcelUrlByJD, formData, { headers: this.tokenHeader, timeout: 0 })
        .then(res => {
          if (res.success) {
            this.$message.success(res.message)
            this.close()
          } else {
            this.$message.warning(res.message)
          }
        })
        .catch(err => {
          console.log('err', err)
        })
        .finally(() => {
          this.uploadModalJD.visible = false
          this.uploadModalJD.loading = false
          this.loadData()
        })
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
